<template>
    <div class="addDialog">
        <el-dialog :close-on-click-modal="false" :close-on-press-escape="false" v-model="visible" title="" :show-close="false" align-center
            class="dialogAdd">
            <div class="dialog_content">
                <div class="dialog_header">
                    <div class="title">
                        <img src="@/assets/images/rectangle07.png" alt="">
                        <span>{{ props.type_dialog == 'add' ? '新增' : '编辑' }}工单类型</span>
                    </div>
                    <!-- <img class="close" src="@/assets/images/close.png" @click="handleClose" alt=""> -->
                </div>
                <div class="dialog_details">
                    <div class="details">
                        <el-form ref="ruleFormRef" class="dialog_form" :model="ruleFormData.ruleForm"
                            :rules="rules">
                            <el-form-item label="工单类型名称" prop="name">
                                <el-input v-model.trim="ruleFormData.ruleForm.name" placeholder=""></el-input>
                            </el-form-item>
                            <el-form-item class="item_btn">
                                <div class="btn">
                                    <el-button type="warning" @click="handleConfirm(ruleFormRef)">保 存</el-button>
                                    <el-button @click="handleClose">取 消</el-button>
                                </div>
                            </el-form-item>
                        </el-form>

                    </div>
                </div>
            </div>

        </el-dialog>
    </div>
</template>

<script setup lang="ts">
import { useTypeAdd, useTypeEdit } from "@/http/device"
import type { FormInstance, FormRules } from 'element-plus'

const props = defineProps({
    dialogVisible: {
        type: Boolean,
        default: false
    },
    type_dialog: { //add 新增 edit 编辑
        type: String,
        default: ''
    },
    dialogParams: {
        type: Object as any,
        default: {}
    },
    checkItem: {
        type: Object as any,
        default: {}
    }
})
let visible = ref(false);

const treeValue = ref<any[]>([]); //空间分类的值


const ruleFormRef = ref<FormInstance>()

interface IruleForm {
    id?: string;
    isSys?: number;
    name: string;
}

const ruleFormData = reactive({
    ruleForm: {
        id: "",
        isSys: 1,//是否系统定义 0-总平台新增 1-运维平台新增
        name: '',//名称
    } as IruleForm
})

watch(() => props.dialogVisible, (newValue, oldValue) => {
    visible.value = newValue;
    if (newValue) {
        // console.log('ruleFormData.ruleForm', ruleFormData.ruleForm);
        if (props.type_dialog == 'add') { //新增分类
            // console.log('checkItem', props.checkItem);
            ruleFormData.ruleForm = {
                isSys: 1,
                name: '',
            }
        } else { //编辑分类
            // console.log('dialogParams',props.dialogParams);
            ruleFormData.ruleForm = JSON.parse(JSON.stringify(props.dialogParams));
        }


        setTimeout(() => {
            if (ruleFormRef.value) {
                ruleFormRef.value.clearValidate();
            }
        }, 50);


    }
});

const rules = reactive<FormRules>({
    name: [{ required: true, message: '请输入使用类型名称', trigger: 'change' }],
})

//emit(父组件中的自定义方法)
const emit = defineEmits(['closeDialog', 'confirmDialog']);
//关闭弹窗
const handleClose = () => {
    emit("closeDialog");
}

//点击保存按钮
const handleConfirm = async (formEl: FormInstance | undefined) => {
    console.log('treeValue', treeValue);

    if (!formEl) return
    await formEl.validate((valid, fields) => {
        if (valid) {
            if (props.type_dialog == 'add') {
                space_Add();
            } else {
                space_edit();
            }
        } else {
            // console.log('error submit!', fields)
        }
    })


}

//新增点击保存
const space_Add = () => {
  
}

//编辑点击保存
const space_edit = () => {
   
}


//点击清空按钮
const empty = (noEmit: any) => {


}

defineExpose({
    empty
})
</script>

<style scoped lang="scss">
.addDialog {
    :deep(.dialogAdd) {
        width: 448px;
        min-height: 235px;
        background: rgba(255, 255, 255, 1);
        border-radius: 2px;

        .el-dialog__header {
            padding: 0;

        }

        .el-dialog__body {
            padding: 0;

            .dialog_content {
                .dialog_header {
                    padding: 0;
                    height: 59px;
                    padding: 8px 32px 4px 62px;
                    line-height: 52px;
                    font-size: 16px;
                    font-weight: 400;
                    color: rgba(0, 0, 0, 1);
                    display: flex;
                    justify-content: space-between;
                    align-items: center;
                    border-bottom: 1px dashed rgba(112, 105, 95, 1);

                    .title {
                        display: flex;
                        align-items: center;

                        img {
                            width: 4px;
                            height: 22px;
                            margin-right: 15px;
                        }

                    }

                    .close {
                        width: 25px;
                        height: 25px;
                        cursor: pointer;

                    }
                }

                .dialog_details {
                    padding: 25px 42px 0 42px;
                    font-size: 16px;
                    font-weight: 400;
                    letter-spacing: 0px;
                    line-height: 30px;
                    color: rgba(0, 0, 0, 1);

                    .details {
                        padding: 0 0 30px 0px;
                        align-items: center;

                        // &.details2 {
                        //     padding: 0 14px 25px 14px;
                        // }

                        .el-input-group--append {
                            .el-input__wrapper {
                                border-right: 0;
                            }

                            .el-input-group__append {
                                background-color: #fff;
                                padding: 0 5px;
                                border-radius: 0;
                            }
                        }

                    }

                }

            }
        }


    }

}

.dialog_form {
    display: block;

    :deep(.el-form-item) {
        // margin-bottom: 22px;

        &.item_btn {
            padding-top: 25px;
        }
        &.is-required {
            .el-form-item__label::before{
                color: red;
                margin-top: 3px;
            }
        }

        .el-form-item__label {
            width: 135px;
            padding-right: 30px;
        }
    }

    .btn {
        width: 100%;
        text-align: right;
        padding-right: 35px;
    }
}
</style>